import { expect } from 'chai';
import { promises } from 'fs';
import sinon from 'sinon';
import { sizeCheck, MAX_REPORT_SIZE } from '../../src/testCase/sizeCheck';

describe('sizeCheck', () => {
    let statStub: sinon.SinonStub;

    beforeEach(() => {
        statStub = sinon.stub(promises, 'stat');
    });

    afterEach(() => {
        statStub.restore();
    });

    it('should throw an error if total size exceeds MAX_REPORT_SIZE', async () => {
        const baseDir = '/some/dir';
        const files = ['file1.txt', 'file2.txt'];

        statStub.onCall(0).resolves({ size: 0.8 * MAX_REPORT_SIZE }); // 8MB
        statStub.onCall(1).resolves({ size: 0.3 * MAX_REPORT_SIZE }); // 3MB

        try{
            await sizeCheck(baseDir, files);
            expect.fail('sizeCheck should throw an error');
        }catch(e:any){
            expect(e.message).to.equal('The report is too large (>10M), abort uploading');
        }
    });

    it('should not throw an error if total size is within limit', async () => {
        const baseDir = '/some/dir';
        const files = ['file1.txt', 'file2.txt'];

        statStub.onCall(0).returns(Promise.resolve({ size: 0.5 * MAX_REPORT_SIZE })); // 5MB
        statStub.onCall(1).returns(Promise.resolve({ size: 0.4 * MAX_REPORT_SIZE })); // 4MB

        try{
            await sizeCheck(baseDir, files);
        }catch(e:any){
            expect.fail('No error should be thrown');
        }
    });

    it('should handle empty file list', async () => {
        const baseDir = '/some/dir';
        const files: string[] = [];

        try{
            await sizeCheck(baseDir, files);
        }catch(e:any){
            expect.fail('No error should be thrown');
        }
    });
});
